package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.base.OmsDeviceTypeDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsDeviceType;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.service.base.DeviceTypeService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DeviceTypeServiceImpl extends BaseService implements DeviceTypeService 
{
	@Autowired
	private OmsDeviceTypeDao omsCommonDaoImpl;
	
	@Override
	public  synchronized JsonCommon<?> add(OmsDeviceType param) 
	{
		try 
		{
			param.setDevice_id(this.genarateId(omsCommonDaoImpl.findMaxId(OmsDeviceType.class), 10));
			omsCommonDaoImpl.add(param);
			return this.buildSuccessJsonCommon(param);
		} 
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> update(OmsDeviceType param) {
		try 
		{
			if (StringUtils.isNotEmpty(param.getFactory_id()))
			{
				param.setFactory_id(null);
			}
			omsCommonDaoImpl.update(param);
			return this.buildSuccessJsonCommon(param);
		} 
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String fid,String code) {
		try 
		{
			if (StringUtils.isEmpty(fid) || 
					StringUtils.isEmpty(code))
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), 
						"Error:the request param is emty.factoryId or code");
			}
			OmsDeviceType param = new OmsDeviceType();
			param.setDevice_code(code);
			param.setFactory_id(fid);;
			omsCommonDaoImpl.LogicDeleteObject(param);
			return this.buildSuccessJsonCommon(param);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}



	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam cparam, OmsDeviceType param) {
		try 
		{
			List<OmsDeviceType> lst = omsCommonDaoImpl.pagingFind(cparam, param);
			int total = 0;
			if (cparam.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(param);
			}
			return this.buildSuccessJsonForList(cparam, lst, total);
		} 
		catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonCommon<?> delete(String did) {
		try {
			omsCommonDaoImpl.deleteById(did, OmsDeviceType.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	/***************20160414 perlin add begin****************************/
	@Override
	public OmsDeviceType findExampleByDeviceId(String device_id) {
		OmsDeviceType queryObj = new OmsDeviceType();
		queryObj.setDevice_id(device_id);
		List<OmsDeviceType> lsDevices = this.omsCommonDaoImpl.findExample(queryObj, null, null,null, null);
		if(lsDevices!=null&&lsDevices.size()>0){
			return lsDevices.get(0);
		}
		else{
			return null;
		}
	}
	/***************20160414 perlin add end****************************/
}